<template>
  <div class="app-container">
    <el-row :gutter="24">

      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="" prop="employeeNum">
            <el-input
              v-model="queryParams.employeeNum"
              placeholder="请输入工号或姓名"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!--<el-row :gutter="10" class="mb8">
          <div class="top-right-btn">
            <el-tooltip class="item" effect="dark" content="刷新" placement="top">
              <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
              <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
            </el-tooltip>
          </div>
        </el-row>-->

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" :max-height="height">
          <!--<el-table-column type="selection" width="50" align="center" />-->
           <el-table-column label="序号" width="70" align="center" fixed="left">
               <template scope="scope"> <span>{{scope.$index + (queryParams.pageNum - 1) * queryParams.pageSize + 1}} </span> </template>
          </el-table-column>
          <!--<el-table-column label="用户编号" align="center" prop="userId"  />-->
          <el-table-column label="工号" align="center" prop="userName" :show-overflow-tooltip="true" min-width="100" />
          <el-table-column label="姓名" align="center" prop="employeeNum" :show-overflow-tooltip="true" />
          <el-table-column label="角色" align="center" prop="userId" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
                <span @click="handleRelation(scope.row)" style="cursor: pointer;text-decoration: underline">查看角色</span>
            </template>
          </el-table-column>
          <el-table-column label="权限" align="center" prop="userId" :show-overflow-tooltip="true">
             <template slot-scope="scope">
                <span @click="getAllSubSystemData(scope.row)" style="cursor: pointer;text-decoration: underline">查看权限</span>
            </template>
          </el-table-column>
          <el-table-column label="机构" align="center" prop="dept.deptName" :show-overflow-tooltip="true" />
          <el-table-column label="服务应用" align="center" prop="userId" :show-overflow-tooltip="true" >
               <template slot-scope="scope">
                <span @click="handleRelation1(scope.row)" style="cursor: pointer;text-decoration: underline">查看服务应用</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 角色对话框 -->
    <el-dialog title="用户角色" :visible.sync="reOpen" width="400px" append-to-body>
      <el-form ref="reForm" :model="reForm" :rules="reRules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <div class="tag-group">
                <el-tag
                    v-for="item in roles"
                    :key="item.roleId"
                    :type="item.roleName"
                    effect="dark">
                    {{ item.roleName }}
                </el-tag>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reOpen=false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 权限对话框 -->
    <el-dialog title="用户权限" :visible.sync="reOpen2" width="600px" append-to-body>
      <el-form ref="reForm2" :model="reForm1" :rules="reRules2" label-width="180px">
        <el-row>
          <el-col :span="24">
                  <el-form-item label="子系统权限" prop="subsystemId">
          <el-select v-model="form.subsystemId" placeholder="请选择" @change="changeChildSystem">
            <el-option v-for="(item,index) in reList" :key="index" :label="item.subsystemName" :value="item.subsystemId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单权限">
          <el-tree
            :data="menuOptions"
            show-checkbox
            ref="menu"
            node-key="id"
            empty-text=""
            :props="defaultProps"
          ></el-tree>
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reOpen2=false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 服务应用对话框 -->
    <el-dialog title="用户服务应用" :visible.sync="reOpen1" width="600px" append-to-body>
      <el-form ref="reForm1" :model="reForm1" :rules="reRules1" label-width="180px">
        <el-row>
          <el-col :span="24">
            <div class="tag-group">
                <el-tag
                    v-for="item in subsysytems"
                    :key="item.subsystemName"
                    :type="item.subsystemName"
                    effect="dark">
                    {{ item.subsystemName }}
                </el-tag>
              </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reOpen1=false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getAuthorizationList,getRolesByUserId,getSubsystemNameByUserId} from "@/api/system/user";
import{ treeselectMenuByUserId } from "@/api/system/role";
import { treeselect as menuTreeselect } from "@/api/system/menu";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "User",
  data() {
    return {
      height: (document.documentElement.clientHeight || document.body.clientHeight)-220,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 角色组
      roles: [],
      subsysytems: [],
      //子系统菜单关联
      reList: [],
      // 菜单列表
      menuOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId:undefined
      },
      reOpen: false,
      reForm: {
          select: undefined,
      },
      reRules: {},
      reOpen1:false,
      reForm1: {
          select: undefined,
      },
      reRules1: {},
      reOpen2:false,
      reForm2: {
          reList: undefined,
      },
      reRules2: {},
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      getAuthorizationList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
     /** 获取当前用户子系统数据 */
    getAllSubSystemData(row){
      this.resetChildSystemDataList();
      this.reOpen2 = true;
      this.reForm2.reList = [];
      this.form.subsystemId = undefined;
        this.queryParams.userId = row.userId;
         getSubsystemNameByUserId(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
           this.reList = response.data;
           if(this.reList && this.reList.length>0){
             this.form.subsystemId = this.reList[0].subsystemId;
             this.changeChildSystem(this.reList[0].subsystemId,row.userId);
           }
        }
      );
    },
    // 重置子系统数据列表
    resetChildSystemDataList(){
      this.$set(this, "menuOptions", []);
    },
    // 关联的子系统数据列表
    changeChildSystem(val,userid){
      this.resetChildSystemDataList();
      var params = { subsystemId: val,userId: userid};
      treeselectMenuByUserId(params).then((res)=>{
        this.menuOptions = res.data;
        var arr = []
        if(this.menuOptions && this.menuOptions.length>0){
          for(var i=0;i<this.menuOptions.length;i++){
            arr.push(this.menuOptions[i].id);
          }
        }
        this.$refs.menu.setCheckedKeys(arr);
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.userName + '"用户吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeUserStatus(row.userId, row.status);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.userTypeOptions, row.userType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        /*nickName: undefined,*/
        password: undefined,
        phonenumber: undefined,
        employeeNum:undefined,
        /*email: undefined,
        sex: undefined,
        status: "0",*/
        remark: undefined,
        postIds: undefined,
        roleIds: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }
    ,
    /** 查看角色按钮操作 */
    handleRelation(row){
        this.reOpen = true;
        this.queryParams.userId = row.userId;
         getRolesByUserId(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.roles = response.data;
        }
      );
    }
     ,
    /** 查看服务应用按钮操作 */
    handleRelation1(row){
        this.reOpen1 = true;
        this.queryParams.userId = row.userId;
         getSubsystemNameByUserId(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.subsysytems = response.data;
        }
      );
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    }
  }
};
</script>
<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
